﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Eagle.Common
{
    /// <summary>
    /// 数据安全转换类
    /// </summary>
    public class DataConvert
    {
        #region 转换为Int32类型
        /// <summary>
        /// 转换为Int32类型,不能转换时返回默认值
        /// </summary>
        /// <param name="value">输入字符串</param>
        /// <param name="defaultValue">转换不成功时的默认值</param>
        /// <returns></returns>
        public static int ToInt32(string value, int defaultValue)
        {
            int number = -1;
            if (Int32.TryParse(value, out number))
                return number;
            return defaultValue;
        }
        /// <summary>
        /// 转换为Int32类型,不能转换时返回默认值
        /// </summary>
        /// <param name="value">输入字符串</param>
        /// <param name="defaultValue">转换不成功时的默认值</param>
        /// <returns></returns>
        public static int? ToInt32(string value, int? defaultValue)
        {
            int number = -1;
            if (Int32.TryParse(value, out number))
                return number as int?;
            return defaultValue;
        }
        #endregion

        #region 转换为bool类型
        /// <summary>
        /// 转换为bool类型,不能转换时返回默认值
        /// </summary>
        /// <param name="value">输入字符串</param>
        /// <param name="defaultValue">转换不成功时的默认值</param>
        /// <returns></returns>
        public static bool ToBoolean(string value, bool defaultValue)
        {
            bool flag = false;
            if (bool.TryParse(value, out flag))
                return flag;
            return defaultValue;
        }
        /// <summary>
        /// 转换为bool类型,不能转换时返回默认值
        /// </summary>
        /// <param name="value">输入字符串</param>
        /// <param name="defaultValue">转换不成功时的默认值</param>
        /// <returns></returns>
        public static bool? ToBoolean(string value, bool? defaultValue)
        {
            bool flag = false;
            if (bool.TryParse(value, out flag))
                return flag as bool?;
            return defaultValue;
        }
        #endregion

        #region 转换为char类型
        /// <summary>
        /// 转换为char类型,不能转换时返回默认值
        /// </summary>
        /// <param name="value">输入字符串</param>
        /// <param name="defaultValue">转换不成功时的默认值</param>
        /// <returns></returns>
        public static char ToChar(string value, char defaultValue)
        {
            char c;
            if (Char.TryParse(value, out c))
                return c;
            return defaultValue;
        }
        #endregion

        #region 转换为sbyte类型
        /// <summary>
        /// 转换为sbyte类型,不能转换时返回默认值
        /// </summary>
        /// <param name="value">输入字符串</param>
        /// <param name="defaultValue">转换不成功时的默认值</param>
        /// <returns></returns>
        public static sbyte ToSByte(string value, sbyte defaultValue)
        {
            sbyte s = 0;
            if (SByte.TryParse(value, out s))
                return s;
            return defaultValue;
        }
        /// <summary>
        /// 转换为sbyte类型,不能转换时返回默认值
        /// </summary>
        /// <param name="value">输入字符串</param>
        /// <param name="defaultValue">转换不成功时的默认值</param>
        /// <returns></returns>
        public static sbyte? ToSByte(string value, sbyte? defaultValue)
        {
            sbyte s = 0;
            if (SByte.TryParse(value, out s))
                return s as sbyte?;
            return defaultValue;
        }
        #endregion

        #region 转换为byte类型
        /// <summary>
        /// 转换为byte类型,不能转换时返回默认值
        /// </summary>
        /// <param name="value">输入字符串</param>
        /// <param name="defaultValue">转换不成功时的默认值</param>
        /// <returns></returns>
        public static byte ToByte(string value, byte defaultValue)
        {
            byte b = 0;
            if (Byte.TryParse(value, out b))
                return b;
            return defaultValue;
        }
        /// <summary>
        /// 转换为byte类型,不能转换时返回默认值
        /// </summary>
        /// <param name="value">输入字符串</param>
        /// <param name="defaultValue">转换不成功时的默认值</param>
        /// <returns></returns>
        public static byte? ToByte(string value, byte? defaultValue)
        {
            byte b = 0;
            if (Byte.TryParse(value, out b))
                return b as byte?;
            return defaultValue;
        }
        #endregion

        #region 转换为Int16类型
        /// <summary>
        /// 转换为Int16类型,不能转换时返回默认值
        /// </summary>
        /// <param name="value">输入字符串</param>
        /// <param name="defaultValue">转换不成功时的默认值</param>
        /// <returns></returns>
        public static Int16 ToInt16(string value, Int16 defaultValue)
        {
            Int16 i16 = 0;
            if (Int16.TryParse(value, out i16))
                return i16;
            return defaultValue;
        }
        /// <summary>
        /// 转换为Int16类型,不能转换时返回默认值
        /// </summary>
        /// <param name="value">输入字符串</param>
        /// <param name="defaultValue">转换不成功时的默认值</param>
        /// <returns></returns>
        public static Int16? ToInt16(string value, Int16? defaultValue)
        {
            Int16 i16 = 0;
            if (Int16.TryParse(value, out i16))
                return i16 as Int16?;
            return defaultValue;
        }
        #endregion

        #region 转换为UInt16类型
        /// <summary>
        /// 转换为UInt16类型,不能转换时返回默认值
        /// </summary>
        /// <param name="value">输入字符串</param>
        /// <param name="defaultValue">转换不成功时的默认值</param>
        /// <returns></returns>
        public static UInt16 ToUInt16(string value, UInt16 defaultValue)
        {
            UInt16 ui16 = 0;
            if (UInt16.TryParse(value, out ui16))
                return ui16;
            return defaultValue;
        }
        /// <summary>
        /// 转换为UInt16类型,不能转换时返回默认值
        /// </summary>
        /// <param name="value">输入字符串</param>
        /// <param name="defaultValue">转换不成功时的默认值</param>
        /// <returns></returns>
        public static UInt16? ToUInt16(string value, UInt16? defaultValue)
        {
            UInt16 ui16 = 0;
            if (UInt16.TryParse(value, out ui16))
                return ui16 as UInt16?;
            return defaultValue;
        }
        #endregion

        #region 转换为UInt32类型
        /// <summary>
        /// 转换为UInt32类型,不能转换时返回默认值
        /// </summary>
        /// <param name="value">输入字符串</param>
        /// <param name="defaultValue">转换不成功时的默认值</param>
        /// <returns></returns>
        public static UInt32 ToUInt32(string value, UInt32 defaultValue)
        {
            UInt32 ui32 = 0;
            if (UInt32.TryParse(value, out ui32))
                return ui32;
            return defaultValue;
        }
        /// <summary>
        /// 转换为UInt32类型,不能转换时返回默认值
        /// </summary>
        /// <param name="value">输入字符串</param>
        /// <param name="defaultValue">转换不成功时的默认值</param>
        /// <returns></returns>
        public static UInt32? ToUInt32(string value, UInt32? defaultValue)
        {
            UInt32 ui32 = 0;
            if (UInt32.TryParse(value, out ui32))
                return ui32 as UInt32?;
            return defaultValue;
        }
        #endregion

        #region 转换为Int64类型
        /// <summary>
        /// 转换为Int64类型,不能转换时返回默认值
        /// </summary>
        /// <param name="value">输入字符串</param>
        /// <param name="defaultValue">转换不成功时的默认值</param>
        /// <returns></returns>
        public static Int64 ToInt64(string value, Int64 defaultValue)
        {
            Int64 i64 = 0;
            if (Int64.TryParse(value, out i64))
                return i64;
            return defaultValue;
        }
        /// <summary>
        /// 转换为Int64类型,不能转换时返回默认值
        /// </summary>
        /// <param name="value">输入字符串</param>
        /// <param name="defaultValue">转换不成功时的默认值</param>
        /// <returns></returns>
        public static Int64? ToInt64(string value, Int64? defaultValue)
        {
            Int64 i64 = 0;
            if (Int64.TryParse(value, out i64))
                return i64 as Int64?;
            return defaultValue;
        }
        #endregion

        #region 转换为UInt64类型
        /// <summary>
        /// 转换为UInt64类型,不能转换时返回默认值
        /// </summary>
        /// <param name="value">输入字符串</param>
        /// <param name="defaultValue">转换不成功时的默认值</param>
        /// <returns></returns>
        public static UInt64 ToUInt64(string value, UInt64 defaultValue)
        {
            UInt64 ui64 = 0;
            if (UInt64.TryParse(value, out ui64))
                return ui64;
            return defaultValue;
        }

        /// <summary>
        /// 转换为UInt64类型,不能转换时返回默认值
        /// </summary>
        /// <param name="value">输入字符串</param>
        /// <param name="defaultValue">转换不成功时的默认值</param>
        /// <returns></returns>
        public static UInt64? ToUInt64(string value, UInt64? defaultValue)
        {
            UInt64 ui64 = 0;
            if (UInt64.TryParse(value, out ui64))
                return ui64 as UInt64?;
            return defaultValue;
        }
        #endregion

        #region 转换为Single类型
        /// <summary>
        /// 转换为Single类型,不能转换时返回默认值
        /// </summary>
        /// <param name="value">输入字符串</param>
        /// <param name="defaultValue">转换不成功时的默认值</param>
        /// <returns></returns>
        public static Single ToSingle(string value, Single defaultValue)
        {
            Single single = 0;
            if (Single.TryParse(value, out single))
                return single;
            return defaultValue;
        }
        /// <summary>
        /// 转换为Single类型,不能转换时返回默认值
        /// </summary>
        /// <param name="value">输入字符串</param>
        /// <param name="defaultValue">转换不成功时的默认值</param>
        /// <returns></returns>
        public static Single? ToSingle(string value, Single? defaultValue)
        {
            Single single = 0;
            if (Single.TryParse(value, out single))
                return single as Single?; 
            return defaultValue;
        }
        #endregion

        #region 转换为Double类型
        /// <summary>
        /// 转换为Double类型,不能转换时返回默认值
        /// </summary>
        /// <param name="value">输入字符串</param>
        /// <param name="defaultValue">转换不成功时的默认值</param>
        /// <returns></returns>
        public static Double ToDouble(string value, Double defaultValue)
        {
            double d = 0;
            if (Double.TryParse(value, out d))
                return d;
            return defaultValue;
        }
        /// <summary>
        /// 转换为Double类型,不能转换时返回默认值
        /// </summary>
        /// <param name="value">输入字符串</param>
        /// <param name="defaultValue">转换不成功时的默认值</param>
        /// <returns></returns>
        public static Double? ToDouble(string value, Double? defaultValue)
        {
            double d = 0;
            if (Double.TryParse(value, out d))
                return d as Double?;
            return defaultValue;
        }
        #endregion

        #region 转换为decimal类型
        /// <summary>
        /// 转换为decimal类型,不能转换时返回默认值
        /// </summary>
        /// <param name="value">输入字符串</param>
        /// <param name="defaultValue">转换不成功时的默认值</param>
        /// <returns></returns>
        public static decimal ToDecimal(string value, decimal defaultValue)
        {
            decimal number = 0m;
            if (decimal.TryParse(value, out number))
                return number;
            return defaultValue;
        }
        /// <summary>
        /// 转换为decimal类型,不能转换时返回默认值
        /// </summary>
        /// <param name="value">输入字符串</param>
        /// <param name="defaultValue">转换不成功时的默认值</param>
        /// <returns></returns>
        public static decimal? ToDecimal(string value, decimal? defaultValue)
        {
            decimal number = 0m;
            if (decimal.TryParse(value, out number))
                return number as decimal?;
            return defaultValue;
        }
        #endregion

        #region 转换为datetime类型
        /// <summary>
        /// 转换为datetime类型,不能转换时返回默认值
        /// </summary>
        /// <param name="value">输入字符串</param>
        /// <param name="defaultValue">转换不成功时的默认值</param>
        /// <returns></returns>
        public static DateTime ToDateTime(string value, DateTime defaultValue)
        {
            DateTime datetime;
            if (DateTime.TryParse(value, out datetime))
                return datetime;
            return defaultValue;
        }
        /// <summary>
        /// 转换为datetime类型,不能转换时返回默认值
        /// </summary>
        /// <param name="value">输入字符串</param>
        /// <param name="defaultValue">转换不成功时的默认值</param>
        /// <returns></returns>
        public static DateTime? ToDateTime(string value, DateTime? defaultValue)
        {
            DateTime datetime;
            if (DateTime.TryParse(value, out datetime))
                return datetime as DateTime?;
            return defaultValue;
        }
        #endregion

        #region 转换为Guid类型
        /// <summary>
        /// 转换为Guid类型,不能转换时返回默认值
        /// </summary>
        /// <param name="value">输入字符串</param>
        /// <param name="defaultValue">转换不成功时的默认值</param>
        /// <returns></returns>
        public static Guid ToGuid(string value, Guid defaultValue)
        {
            if (value.Length !=36)
                return defaultValue;
            return new Guid(value);
        }
        #endregion

        #region 泛型数据类型转换

        /// <summary>
        /// 泛型数据类型转换
        /// </summary>
        /// <typeparam name="T">自定义数据类型</typeparam>
        /// <param name="value">传入需要转换的值</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns></returns>
        public static T DataType<T>(object value, T defaultValue)
        {
            return (T)ConvertToT<T>(value, defaultValue);
        }
        /// <summary>
        /// 转换数据类型
        /// </summary>
        /// <typeparam name="T">自定义数据类型</typeparam>
        /// <param name="myvalue">传入需要转换的值</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns></returns>
        private static object ConvertToT<T>(object myvalue, T defaultValue)
        {
            TypeCode typeCode = System.Type.GetTypeCode(typeof(T));
            if (myvalue == null)
                return defaultValue;
            string value = myvalue.ToString();
            switch (typeCode)
            {
                case TypeCode.Boolean:
                    bool flag = false;
                    if (bool.TryParse(value, out flag))
                        return flag;
                    return defaultValue;
                case TypeCode.Char:
                    char c;
                    if (Char.TryParse(value, out c))
                        return c;
                    return defaultValue;
                case TypeCode.SByte:
                    sbyte s = 0;
                    if (SByte.TryParse(value, out s))
                        return s;
                    return defaultValue;
                case TypeCode.Byte:
                    byte b = 0;
                    if (Byte.TryParse(value, out b))
                        return b;
                    return defaultValue;
                case TypeCode.Int16:
                    Int16 i16 = 0;
                    if (Int16.TryParse(value, out i16))
                        return i16;
                    return defaultValue;
                case TypeCode.UInt16:
                    UInt16 ui16 = 0;
                    if (UInt16.TryParse(value, out ui16))
                        return ui16;
                    return defaultValue;
                case TypeCode.Int32:
                    int i = 0;
                    if (Int32.TryParse(value, out i))
                        return i;
                    return defaultValue;
                case TypeCode.UInt32:
                    UInt32 ui32 = 0;
                    if (UInt32.TryParse(value, out ui32))
                        return ui32;
                    return defaultValue;
                case TypeCode.Int64:
                    Int64 i64 = 0;
                    if (Int64.TryParse(value, out i64))
                        return i64;
                    return defaultValue;
                case TypeCode.UInt64:
                    UInt64 ui64 = 0;
                    if (UInt64.TryParse(value, out ui64))
                        return ui64;
                    return defaultValue;
                case TypeCode.Single:
                    Single single = 0;
                    if (Single.TryParse(value, out single))
                        return single;
                    return defaultValue;
                case TypeCode.Double:
                    double d = 0;
                    if (Double.TryParse(value, out d))
                        return d;
                    return defaultValue;
                case TypeCode.Decimal:
                    decimal de = 0;
                    if (Decimal.TryParse(value, out de))
                        return de;
                    return defaultValue;
                case TypeCode.DateTime:
                    DateTime dt;
                    if (DateTime.TryParse(value, out dt))
                        return dt;
                    return defaultValue;
                case TypeCode.String:
                    if (value.Length == 0)
                        return "";
                    return value.ToString();
            }
            throw new ArgumentNullException("defaultValue", "不能为Empty,Object,DBNull");
        }
        #endregion
    }
}
